package cn.iocoder.yudao.module.shop.category.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.api.shop.category.ShopCategoryApi;
import cn.iocoder.yudao.module.api.shop.category.eums.ShopCategoryCodeEnum;
import cn.iocoder.yudao.module.shop.category.pojo.po.ShopCategory;
import cn.iocoder.yudao.module.shop.category.service.ShopCategoryService;
import cn.iocoder.yudao.module.shop.info.service.ShopInfoService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 *  这里是文件说明
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2024/12/23
 */
@Service
@Validated
public class ShopCategoryApiImpl implements ShopCategoryApi {
    @Resource
    private ShopCategoryService shopCategoryService;
    @Resource
    private ShopInfoService shopInfoService;

    @Override
    public String getProductTypeByShopId(Long shopId) {
        ShopCategory shopCategory = shopInfoService.getShopCategory(shopId);
        if (shopCategory == null || StrUtil.isBlank(shopCategory.getCode())) {
            return ShopCategoryCodeEnum.OTHER.getCode();
        }
        return shopCategory.getCode();
    }

    @Override
    public Set<Long> getIdsByO2O() {
        return shopCategoryService.getIdsByO2O();
    }

    @Override
    public Set<Long> getEnableDescendentCategoryIds(String parentCode, boolean includeParent) {
        List<ShopCategory> list = shopCategoryService.getEnableDescendentCategoryList(parentCode, includeParent);
        if (CollUtil.isEmpty(list)) {
            return CollUtil.newHashSet();
        }
        return list.stream().map(ShopCategory::getId).collect(Collectors.toSet());
    }

    @Override
    public Set<Long> getEnableDescendentCategoryIds(Long parentId) {
        List<ShopCategory> list = shopCategoryService.getEnableDescendentCategoryList(parentId);
        if (CollUtil.isEmpty(list)) {
            return CollUtil.newHashSet();
        }
        return list.stream().map(ShopCategory::getId).collect(Collectors.toSet());
    }

    @Override
    public long getIdBySupplyRelatedId(Long supplyRelatedId) {
        if (supplyRelatedId == null || supplyRelatedId == 0) {
            return 0L;
        }
        ShopCategory category = shopCategoryService.getBySupplyRelatedId(supplyRelatedId);
        if (category != null) {
            return category.getId();
        }
        return 0L;
    }

}
